#include "test_keypad.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "cm_pm.h"
#include "cm_os.h"
//#include "FreeRTOS.h"


#define TEST_TIME_PWRKEY_PRESS (2500 / (portTICK_PERIOD_MS))                    //2.5秒

static osTimerId_t test_timer_pwrkey = NULL; 

//keypad事件回调函数使用示例
void test_keypad_callback1(cm_keypad_map_t id, cm_keypad_event_e evt)
{
    /* 用户在此处实现逻辑，本处为Demo示例，使用串口打印keypad事件*/
    if (CM_KEY_EVENT_PRESS == evt)
    {
        cm_demo_printf("cb1 key[%#x] is pressed\n", id);            //实际按键的key id由硬件电路决定，客户使用前建议先执行按键操作获取键值后再撰写代码逻辑
    }
    else if (CM_KEY_EVENT_RELEASE == evt)
    {
        cm_demo_printf("cb1 key[%#x] is released\n", id); 
    }
    else
    {
        cm_demo_printf("cb1 key[%#x] error, state[%d]\n", id, evt); 
    }
}

void test_keypad_callback2(cm_keypad_map_t id, cm_keypad_event_e evt)
{
    /* 用户在此处实现逻辑，本处为Demo示例，使用串口打印keypad事件*/
    if (CM_KEY_EVENT_PRESS == evt)
    {
        cm_demo_printf("cb2 key[%#x] is pressed\n", id);            //实际按键的key id由硬件电路决定，客户使用前建议先执行按键操作获取键值后再撰写代码逻辑
    }
    else if (CM_KEY_EVENT_RELEASE == evt)
    {
        cm_demo_printf("cb2 key[%#x] is released\n", id); 
    }
    else
    {
        cm_demo_printf("cb2 key[%#x] error, state[%d]\n", id, evt); 
    }
}

void test_keypad_callback3(cm_keypad_map_t id, cm_keypad_event_e evt)
{
    /* 用户在此处实现逻辑，本处为Demo示例，使用串口打印keypad事件*/
    if (CM_KEY_EVENT_PRESS == evt)
    {
        cm_demo_printf("cb3 key[%#x] is pressed\n", id);            //实际按键的key id由硬件电路决定，客户使用前建议先执行按键操作获取键值后再撰写代码逻辑
    }
    else if (CM_KEY_EVENT_RELEASE == evt)
    {
        cm_demo_printf("cb3 key[%#x] is released\n", id); 
    }
    else
    {
        cm_demo_printf("cb3 key[%#xd] error, state[%d]\n", id, evt); 
    }
}

//PwrKey长按事件执行示例
// static void test_timerTimePwrkeyHandler(void * arg)
// {
//     cm_demo_printf("pwrkey is pressed for 2.5 seconds, power off\n");
//     osDelay(10);
//     cm_pm_reboot(CM_REBOOT_MODE_POWER_OFF);
//     osTimerDelete(test_timer_pwrkey);
//     test_timer_pwrkey = NULL;
// }

// //PwrKey事件回调函数使用示例
// static void test_pwrkey_callback(cm_keypad_event_e evt)
// {
//     /* 用户在此处实现逻辑，本处为Demo示例*/
//     if (CM_KEY_EVENT_PRESS == evt)
//     {
//         cm_demo_printf("pwrkey is pressed\n");
        
//         if (NULL == test_timer_pwrkey)
//         {
//             test_timer_pwrkey = osTimerNew(test_timerTimePwrkeyHandler, osTimerOnce, 0 ,0);

//             if (NULL == test_timer_pwrkey)
//             {
//                 cm_demo_printf("Timer create fail, do nothing\n");
//             }
//         }
        
//         osTimerStart(test_timer_pwrkey, TEST_TIME_PWRKEY_PRESS);
//     }
//     else if (CM_KEY_EVENT_RELEASE == evt)
//     {
//         cm_demo_printf("pwrkey is released\n"); 
        
//         if (NULL != test_timer_pwrkey)
//         {
//             osTimerStop(test_timer_pwrkey);
//             osTimerDelete(test_timer_pwrkey);
//             test_timer_pwrkey = NULL;
//         }
//     }
//     else
//     {
//         cm_demo_printf("pwrkey error, state[%d]\n", evt); 
//     }
// }


void cm_test_keypad_operation(unsigned char*cmd[],int len)
{
    if (0 == strncmp("CFG",cmd[2],strlen("CFG")))
    {
        cm_keypad_kp_mkin_e col[CM_KP_MKI_NO_VALID] = {CM_KP_MKI_NO_VALID};
        cm_keypad_kp_mkout_e row[CM_KP_MKI_NO_VALID] = {CM_KP_MKI_NO_VALID};
        uint8_t col_len = (uint8_t)atoi((char *)cmd[3]);
        uint8_t row_len = (uint8_t)atoi((char *)cmd[3+col_len+1]);
        uint8_t i = 0;
        uint8_t keypad_value;
        cm_keypad_cfg_t cm_keypad_cfg = {0};
        
        cm_demo_printf("cm keypad config col X row(%d X %d)\n",col_len,row_len);
        
        cm_demo_printf("cm keypad config col:");
        for(i=0;i<col_len;i++)
        {
            keypad_value = (uint8_t)atoi((char *)cmd[4+i]);
	        col[i] = keypad_value;
	        cm_demo_printf("%d ",col[i]);
        }
        cm_demo_printf("\n");
        
        cm_demo_printf("cm keypad config row:");
        for(i=0;i<row_len;i++)
        {
            keypad_value = (uint8_t)atoi((char *)cmd[3+col_len+2+i]);
            row[i] = keypad_value;
            cm_demo_printf("%d ",row[i]);
        }
        cm_demo_printf("\n");
        
        cm_keypad_cfg.cm_col = col;
        cm_keypad_cfg.cm_row = row;
        cm_keypad_cfg.cm_col_len = col_len;
        cm_keypad_cfg.cm_row_len = row_len;

        cm_keypad_config(&cm_keypad_cfg);

    }
    else if(0 == strncmp("INIT",cmd[2],strlen("INIT")))
    {
        int32_t keypad_init = cm_keypad_init();
        cm_demo_printf("keypad init ret:%d\n",keypad_init);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,keypad_init,cm_keypad_init);
    }
    else if(0 == strncmp("REG",cmd[2],strlen("REG")))
    {
        cm_demo_printf("keypad reg1 ret:%d\n", cm_keypad_register(test_keypad_callback1));
        cm_demo_printf("keypad reg2 ret:%d\n", cm_keypad_register(test_keypad_callback2));
        int32_t keypad_register = cm_keypad_register(test_keypad_callback3);
        cm_demo_printf("keypad reg3 ret:%d\n",keypad_register);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,keypad_register,cm_keypad_register);
//        cm_demo_printf("pwrkey reg ret:%d\n",cm_pwrkey_register(test_pwrkey_callback));
    }
    else if(0 == strncmp("UNREG",cmd[2],strlen("UNREG")))
    {
        int32_t keypad_unregister = cm_keypad_unregister(test_keypad_callback1);
        cm_demo_printf("keypad unregiser ret:%d\n",keypad_unregister);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,keypad_unregister,cm_keypad_unregister);
    }else if(0 == strncmp("DEINIT",cmd[2],strlen("DEINIT")))
    {
        cm_keypad_deinit();
        cm_demo_printf("cm_keypad_deinit no return\n");
    }
    else{
        cm_demo_printf("param error!\n");
    }
}
